﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Kaleida.ServiceMonitor.Model.Parsing;

namespace Kaleida.UnitTests.Scripts
{
    [TestFixture]
    public class ExampleScriptBuildTests
    {
        [Test]
        public void TestAllExampleScriptsCanBeBuilt()
        {
            var rootDirectory = Environment.CurrentDirectory;

            ScriptParser.Initialise(Environment.CurrentDirectory);

            Trace.WriteLine(string.Format("Searching '{0}' and sub directories for *.mscr files", rootDirectory));

            var paths = Directory.GetFiles(rootDirectory, "*.mscr", SearchOption.AllDirectories);

            foreach(var path in paths)
            {
                Trace.WriteLine("Reading, Parsing and building " + path);
                var scriptSource = File.ReadAllText(path);
                var defaultName = Path.GetFileNameWithoutExtension(path);
                var executableScript = scriptSource.ParseAndBuild(defaultName);
                Assert.IsTrue(executableScript.AllScriptModules.Any(i=>i.Operations.Any()), "Expected '{0}' to have at least one operation", path);
            }
        }
    }
}
